﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data.SqlClient;

namespace CitizenDiary.Sync.Contract
{
    [DataContract]
    public class SynchronizationCollection : Dictionary<Guid, SynchronizationItem>
    {
        public SynchronizationCollection()
        {
        }

        public SynchronizationCollection(IEnumerable<KeyValuePair<Guid, SynchronizationItem>> items)
        {
            items.All((item) =>
                {
                    Add(item.Key, item.Value);
                    return true;
                });
        }
    }

    [DataContract]
    public class SynchronizationItem
    {
        [DataMember]
        public SynchronizationType Type { get; set; }

        [DataMember]
        public string Content { get; set; }

        [DataMember]
        public Guid UpdatedDeviceID { get; set; }

        [DataMember]
        public long UpdatedVersion { get; set; }

        [DataMember]
        public bool IsDeleted { get; set; }

        [DataMember]
        public DateTime UpdatedOn { get; set; }

        public SynchronizationItem()
        {
        }

        public SynchronizationItem(SynchronizationItem @base)
            : this(@base, @base.Type)
        {
        }

        public SynchronizationItem(SynchronizationItem @base, SynchronizationType type)
        {
            Type = type;
            Content = @base.Content;
            UpdatedVersion = @base.UpdatedVersion;
            UpdatedDeviceID = @base.UpdatedDeviceID;
            IsDeleted = @base.IsDeleted;
            UpdatedOn = @base.UpdatedOn;
        }

        public SynchronizationItem(SqlDataReader reader, SynchronizationType type = SynchronizationType.Ignore)
        {
            Type = type;
            Content = (string)reader["Content"];
            UpdatedVersion = (long)reader["UpdatedVersion"];
            UpdatedDeviceID = (Guid)reader["UpdatedDeviceID"];
            IsDeleted = (bool)reader["IsDeleted"];
            UpdatedOn = (DateTime)reader["UpdatedOn"];
        }
    }
}
